package com.vf.s.mvc.sync.common.task;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

import com.jfinal.log.Log;
import com.vf.s.common.model.sync.BizSyncDataTask;
import com.vf.s.mvc.sync.common.task.http.HttpTaskHandle;

public class Cron4jHttp implements Runnable {
	
	protected final Log LOG = Log.getLog(getClass());
	private ExecutorService executor = Executors.newFixedThreadPool(100);
	public void run() {
		List<BizSyncDataTask> tasks=BizSyncDataTask.dao.find("SELECT * FROM "+BizSyncDataTask.TABLE_NAME+" WHERE status='1'");
		if(tasks!=null  && tasks.size()>0) {
			for(BizSyncDataTask task:tasks) {
				CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
					@Override
					public Integer get() {
						try {
							HttpTaskHandle.handle(task);
						} catch (Exception e) {
							e.printStackTrace();
						}
						return 1;
					}
				}, executor);
				future.thenAccept(e -> {
					System.out.println(e);
				});
			}
		}else {
			LOG.debug("无数据同步任务！！！");
		}
	}
}
